/**
 * Created by leiqr on 2015/10/08.
 */
define(function (require, exports, module) {
    var $ = require('jquery');
    require("angular-ui");
    var ui = require("ui");
    require("./jqueryui_sortable");
    var template = require("tmpl");
    require("permission");
    var angular = require("angular");
    require("angular-interceptor");

    var urlPrefix = window['G_CONFIG']['api_prefix'] + 'qmyx/broker/';

    angular.module('app', ['ngInterceptor', 'ngUI'])
        .factory('httpPost', ['$http', function ($http) {
            return function (url, data) {
                if (!data) {
                    data = {};
                }
                data = $.extend(data, {"USE_FORM_DATA": false});
                return $http.post(url, data);
            }
        }])
        .service('callsettingService', ['$q', 'httpPost', function ($q, httpPost) {
            var service = {
                getProjectList: function () {
                    return function () {
                        var deferred = $q.defer();
                        $http.get(urlPrefix + 'get')
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    deferred.resolve(res.data);
                                }
                            });
                        return deferred.promise;
                    }
                },
                getSettings: function (queryData) {
                    var deferred = $q.defer();
                    httpPost(urlPrefix, queryData)
                        .success(function (res) {
                            if (res.retCode == 0) {
                                deferred.resolve(res.data);
                            }
                        });
                    return deferred.promise;
                },
                saveSettings: function (queryData) {
                    var deferred = $q.defer();
                    httpPost(urlPrefix, queryData)
                        .success(function (res) {
                            if (res.retCode == 0) {
                                deferred.resolve(res.data);
                            }
                        });
                    return deferred.promise;
                }
            };
            return service;
        }])
        .controller('MainController', ['$rootScope', '$scope', 'callsettingService',
            function ($rootScope, $scope, callsettingService) {


                //callsettingService.getProjectList().then(function(data){
                //    $scope.projectList = data;
                //});

                var tplData = {
                    "currRight": 0,
                    "webUrl": "http://ydxsbg.myscrm.com.cn",
                    "theme": "default",
                    "pub": "http://ydxsbg.myscrm.com.cn/modules/init/themes/pub",
                    "is_refactor_version": true,
                    "current_http_params": ["admin/init/custom-page/page-setting", {
                        "token": "skelku1407290903",
                        "name": "编辑客户资料",
                        "is_group": "1",
                        "is_view": "0",
                        "table": "s_customer,s_opportunity",
                        "page_code": "customer_info"
                    }],
                    "ProjList": {
                        "tree": {
                            "39d1ac02-993f-b107-908b-19e601dae78e": {
                                "children": [{
                                    "name": "黄礼测试",
                                    "projid": "39d1ac02-993f-b107-908b-19e601dae78e"
                                }]
                            },
                            "39cabb19-8a9b-a1f2-414e-b7f019fa06fc": {
                                "children": [{
                                    "name": "深圳第五园",
                                    "projid": "39cabb19-8a9b-a1f2-414e-b7f019fa06fc"
                                }]
                            },
                            "39d1610d-7e67-21d4-5e14-c379c211f5d3": {
                                "children": [{
                                    "name": "测试测试",
                                    "projid": "39d1610d-7e67-21d4-5e14-c379c211f5d3"
                                }]
                            },
                            "39d29507-be9a-9476-1f79-fc746fa855ad": {
                                "children": [{
                                    "name": "AAA",
                                    "projid": "39d29507-be9a-9476-1f79-fc746fa855ad"
                                }]
                            }
                        },
                        "default_selected": {"name": "黄礼测试", "projid": "39d1ac02-993f-b107-908b-19e601dae78e"},
                        "selected_proj_id": "39d1ac02-993f-b107-908b-19e601dae78e"
                    },
                    "unique_id": "4pvgumglcuavtkm4lsn9aemmj5|20151008104446",
                    "proj_id": "39d1ac02-993f-b107-908b-19e601dae78e",
                    "token": "skelku1407290903",
                    "selected_fields": [{
                        "groupname": "基本信息",
                        "ismove": 0,
                        "list": {
                            "s_customer.cst_name": {
                                "name": "姓名",
                                "field": "s_customer.cst_name",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 1,
                                "is_sys": 0
                            },
                            "s_customer.gender": {
                                "name": "性别",
                                "field": "s_customer.gender",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.mobile_tel": {
                                "name": "手机",
                                "field": "s_customer.mobile_tel",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 1,
                                "is_sys": 0
                            },
                            "s_opportunity.label_ids": {
                                "name": "标签",
                                "field": "s_opportunity.label_ids",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.desc": {
                                "name": "备注",
                                "field": "s_opportunity.desc",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            }
                        }
                    }, {
                        "groupname": "客户意向",
                        "ismove": 1,
                        "list": {
                            "s_opportunity.cst_from": {
                                "name": "客户来源",
                                "field": "s_opportunity.cst_from",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.follow_level": {
                                "name": "购房意向",
                                "field": "s_opportunity.follow_level",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.area_wishe_ids": {
                                "name": "需求面积",
                                "field": "s_opportunity.area_wishe_ids",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.floor_wishe_ids": {
                                "name": "意向楼层",
                                "field": "s_opportunity.floor_wishe_ids",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.using_id": {
                                "name": "置业目的",
                                "field": "s_opportunity.using_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.attention_ids": {
                                "name": "最关注方面",
                                "field": "s_opportunity.attention_ids",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.attention_ids2": {
                                "name": "关注因素2",
                                "field": "s_opportunity.attention_ids2",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.attention_ids3": {
                                "name": "关注因素3",
                                "field": "s_opportunity.attention_ids3",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.known_id": {
                                "name": "认知途径9999",
                                "field": "s_opportunity.known_id",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.wishprice": {
                                "name": "购房预算",
                                "field": "s_opportunity.wishprice",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_opportunity.is_lose": {
                                "name": "是否无效",
                                "field": "s_opportunity.is_lose",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 1
                            },
                            "s_opportunity.lose_reason": {
                                "name": "无效原因",
                                "field": "s_opportunity.lose_reason",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 1
                            }
                        }
                    }, {
                        "groupname": "客户特征",
                        "ismove": 1,
                        "list": {
                            "s_customer.ages": {
                                "name": "年龄段1125",
                                "field": "s_customer.ages",
                                "isrequired": 1,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.home_id": {
                                "name": "居住区域",
                                "field": "s_customer.home_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.work_area_id": {
                                "name": "工作区域",
                                "field": "s_customer.work_area_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.work_type_id": {
                                "name": "工作行业",
                                "field": "s_customer.work_type_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.marriage_id": {
                                "name": "婚姻状况",
                                "field": "s_customer.marriage_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.family_id": {
                                "name": "家庭结构",
                                "field": "s_customer.family_id",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.cardid": {
                                "name": "证件号码",
                                "field": "s_customer.cardid",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.cardtype": {
                                "name": "证件类型",
                                "field": "s_customer.cardtype",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.mainbusiness": {
                                "name": "主营业态",
                                "field": "s_customer.mainbusiness",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.detailaddress": {
                                "name": "详细地址",
                                "field": "s_customer.detailaddress",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            },
                            "s_customer.IdNumber": {
                                "name": "身份证",
                                "field": "s_customer.IdNumber",
                                "isrequired": 0,
                                "isenble_del": 0,
                                "ismust_required": 0,
                                "is_sys": 0
                            }
                        }
                    }],
                    "option_fields": {
                        "s_customer.house_count_id": {
                            "name": "置业次数",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.is_hongkong": {
                            "name": "香港籍",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.is_local": {
                            "name": "本地户籍",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.insurance": {
                            "name": "有无社保",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.allow_pay": {
                            "name": "购房资格",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.winder": {
                            "name": "所属区域",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.customerbiaoqian": {
                            "name": "客户特征标签123",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.yucz01": {
                            "name": "客户可选字段",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.homephone": {
                            "name": "家庭电话",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.type_wishe_ids": {
                            "name": "需求户型",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.product_purpose_id": {
                            "name": "意向产品",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.yx_proj_guid": {
                            "name": "意向项目",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.beizhu": {
                            "name": "备注说明",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.mobile_tel_bak": {
                            "name": "手机2",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.DFCX": {
                            "name": "到访次数",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.XMTZ": {
                            "name": "相貌特征",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.YQDJ": {
                            "name": "预期单价",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.KHDJ": {
                            "name": "客户等级",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.CYYXHD": {
                            "name": "参与营销活动",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.JTRS": {
                            "name": "家庭人数",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.FAX": {
                            "name": "传真",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.XQYT": {
                            "name": "需求业态",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.GJXW": {
                            "name": "关键行为",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.wtry": {
                            "name": "外拓人员",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.cs": {
                            "name": "转访模式",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.dzf": {
                            "name": "电转访",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.xmkx": {
                            "name": "项目抗性",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.suoshufenxiao": {
                            "name": "所属分销公司",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.xqts": {
                            "name": "需求套数",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.jzds": {
                            "name": "竞争对手客户类型",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.dingzhi": {
                            "name": "我是定制",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_gzdw": {
                            "name": "工作单位俞定制表",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_age": {
                            "name": "年龄1",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_age2": {
                            "name": "年龄2",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_nianlingluru": {
                            "name": "年龄录入",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_age3": {
                            "name": "年龄3",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_label_ids_bak": {
                            "name": "肤色",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_fkfs": {
                            "name": "付款方式",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_wlwl": {
                            "name": "意向业态",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_fkfs_znjw": {
                            "name": "中南付款方式",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.price_wishe_ids": {
                            "name": "意向价格",
                            "isenble_del": 0,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_genjinfangshi": {
                            "name": "跟进方式",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_likeornot": {
                            "name": "喜不喜欢",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_kaifashan": {
                            "name": "开发商名称",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_gognsifuzren": {
                            "name": "公司负责人",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_fuzrendianhua": {
                            "name": "负责人电话",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_gongsidizhi": {
                            "name": "公司地址",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xiangmu": {
                            "name": "曾开发项目",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_gongsi": {
                            "name": "曾合作公司",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xiangmumc": {
                            "name": "项目名称",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xiangmufuzeren": {
                            "name": "项目负责人",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xmfzrdianhua": {
                            "name": "项目负责人电话",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xiangmudizhi": {
                            "name": "项目地址",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_tujing": {
                            "name": "途径",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_moshi": {
                            "name": "销售模式",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xingzhi": {
                            "name": "公司性质",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_zczj": {
                            "name": "注册资金",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_kaifazizhi": {
                            "name": "开发资质",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_xianz": {
                            "name": "项目现状",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_mqhzgs": {
                            "name": "目前合作公司",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_yixiangjibie": {
                            "name": "意向级别",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_ceshidanxuan": {
                            "name": "测试单选",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_bdfc": {
                            "name": "本地房产",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_sflt": {
                            "name": "AA是否老客户推荐",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_jjh": {
                            "name": "全通用",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst01": {
                            "name": "家属姓名",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst02": {
                            "name": "房间1",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp01": {
                            "name": "qq",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst03": {
                            "name": "ww",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst04": {
                            "name": "首次认知途径",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp02": {
                            "name": "客户类型1",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp03": {
                            "name": "关注期数老客户姓名房号",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst05": {
                            "name": "公司名称",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp04": {
                            "name": "意向面积",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp05": {
                            "name": "单价报价是否符合客户心理预期",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp06": {
                            "name": "现有户型面积与客户需求是否匹配",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp07": {
                            "name": "资金额是否充足是否符合银行贷款资格",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp08": {
                            "name": "是否家庭主事人或决策者",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp09": {
                            "name": "客户等级",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp10": {
                            "name": "是否有比较竞品或关注市场走势",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp11": {
                            "name": "意向级别",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst06": {
                            "name": "购房用途",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst07": {
                            "name": "抗性",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp12": {
                            "name": "兴趣点",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp13": {
                            "name": "是否有车2",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp14": {
                            "name": "是否企业主2",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp15": {
                            "name": "意向房型",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst08": {
                            "name": "下次跟进时间",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp16": {
                            "name": "是否有车",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp17": {
                            "name": "是否企业主",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_opportunity.ex_opp18": {
                            "name": "转访模式",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst09": {
                            "name": "客户属相",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst10": {
                            "name": "AA",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst11": {
                            "name": "燕语",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst12": {
                            "name": "出生日期",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst13": {
                            "name": "测试6",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst14": {
                            "name": "置业顾问",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst15": {
                            "name": "来自区域",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst16": {
                            "name": "多个跟进人",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        },
                        "s_customer.ex_cst17": {
                            "name": "多跟进人",
                            "isenble_del": 1,
                            "isrequired": 0,
                            "ismust_required": 0,
                            "is_sys": 0
                        }
                    },
                    "page_code": "customer_info",
                    "is_group": "1",
                    "table": "s_customer,s_opportunity",
                    "action": "page-setting",
                    "urlparams": {
                        "token": "skelku1407290903",
                        "name": "编辑客户资料",
                        "is_group": "1",
                        "is_view": "0",
                        "table": "s_customer,s_opportunity",
                        "page_code": "customer_info",
                        "__orgcode": "kktyadmin"
                    },
                    "isAndroid": 0,
                    "ticket": "39d35772-4a40-f533-7d60-ed3995c9e11a"
                };


                template.helper('prop2data', function (obj) {
                    var dataHtml = [];
                    for (var item in obj) {
                        dataHtml.push('data-' + item + '=' + obj[item]);
                    }
                    dataHtml.push('data-is_view=' + tplData.urlparams.is_view);
                    return dataHtml.join(' ');
                });

                function initSettingsData(){
                    // 初始化数据
                    $('#avai-fields').html(template('tpl-avai-fields', tplData));
                    $('#selected-fields').html(template('tpl-selected-fields', tplData));
                    $('#avai-fields,#selected-fields').disableSelection();
                    // 已选字段拖动排序
                    $(".sortable").sortable({
                        connectWith: ".sortable",
                        placeholder: "field-placeholder",
                        handle: ".arrow-move"
                    }).disableSelection();
                }


                initSettingsData();

                var leftLiTpl = '<li {{v | prop2data}}>\
                                {{v.name}}\
                                <div class="fr">\
                                    <a><i class="icon-op circle-arrow-right move2right" title="添加到已选字段" aria-hidden="true"></i></a>\
                                </div>\
                                </li>';
                var rightLiTpl = '<li {{v | prop2data}}>\
                                {{if v.is_view==0&&v.isrequired==1}}\
                                    <em class="required">*</em>\
                                {{/if}}\
                                    {{v.name}}\
                                {{if v.is_view==0&&v.ismust_required==0}}\
                                    <a href="javascript:;" class="ml20 c-orange action-require operate">\
                                        {{if v.isrequired==1}}取消必填{{/if}}{{if v.isrequired==0}}设为必填{{/if}}\
                                    </a>\
                                {{/if}}\
                                <div class="fr w70">\
                                {{if v.is_sys==0}}\
                                    <a><i class="icon-op circle-arrow-left move2left" title="移除" aria-hidden="true"></i></a>\
                                {{/if}}\
                                {{if v.ismove==1}}\
                                    <a class="fr"><i class="icon-op arrow-move" title="点击拖动可排序" aria-hidden="true"></i></a>\
                                {{/if}}\
                                </div>\
                                </li>';

                // 特殊处理字段数组，顺序很重要
                var specialFields = [
                    's_customer.cst_name',
                    's_customer.gender',
                    's_customer.mobile_tel',
                    's_opportunity.label_ids',
                    's_opportunity.desc'
                ];
                // 特殊处理的页面，顺序无关
                var specialPages = [
                    'customer_info',
                    'customer_view'
                ];

                $('#avai-fields').on('click', '.move2right', function (e) {
                    // 备选字段移向已选字段
                    var me = $(this);
                    var li = me.closest('li');
                    move2Right(li);
                });

                // 设为必填、取消必填
                $('#selected-fields').on('click', '.action-require', function () {
                    var li = $(this).closest('li');
                    var data = li.data();
                    if (data.isrequired == 1) {
                        data.isrequired = 0;
                    } else {
                        data.isrequired = 1;
                    }
                    var rightLi = template.compile(rightLiTpl)({v: data});
                    // 左边相应的设为必填
                    $('#avai-fields li[data-field="' + data.field + '"]').data('isrequired', data.isrequired);
                    li.replaceWith(rightLi);
                });
                // 已选字段移至备用字段
                $('#selected-fields').on('click', '.move2left', function () {
                    var me = $(this);
                    var li = me.closest('li');
                    move2Left(li);
                });

                $scope.readSetting = function () {

                };

                $scope.save = function () {

                    var selected = []; // 已选的所有字段，不分组
                    var group = [];  // 已选的所有字段，按分组
                    var required = []; // 已选字段中的所有必填字段
                    var uls = $('#selected-fields').find('ul');
                    uls.each(function (i) {
                        var ul = $(this);
                        var groupname = ul.data('groupname');
                        var ismove = ul.data('ismove');
                        var arrList = [];
                        ul.find('li').each(function () {
                            var field = $(this).data('field');
                            var isrequired = $(this).data('isrequired');
                            if (isrequired == 1) {
                                required.push(field);
                            }
                            arrList.push(field);
                            selected.push(field);
                        });
                        var list = arrList.join(',');
                        group.push({
                            groupname: groupname,
                            ismove: ismove,
                            list: list
                        });
                    });
                    var data = {
                        required: required.join(','),
                        group: JSON.stringify(group),
                        selected: selected.join(','),
                        page_code: tplData.urlparams.page_code
                    };
                    $.ajax({
                        url: '',
                        type: 'post',
                        dataType: 'json',
                        data: data,
                        success: function (data) {
                            if (data.isSuccess == 1) {
                                ui.showSucTip();
                            } else {
                                ui.showErrTip();
                            }

                        },
                        error: function () {
                            ui.showErrTip();
                        }
                    });


                };

                $scope.cancel = function () {

                };

                // 从备选移动到已选字段，传入
                function move2Right(ele) {
                    var data = ele.data();
                    // 必须必填就必填
                    if (data.ismust_required == '1') {
                        data.isrequired = 1;
                    }
                    var curFieldPos = $.inArray(data.field, specialFields);
                    var curPagePos = $.inArray(tplData.page_code, specialPages);
                    // 对于客户资料编辑页面 specialFields内的字段移到右边要不能拖动
                    if (curPagePos >= 0 && curFieldPos >= 0) {
                        data.ismove = 0;
                    }
                    var rightLi = $(template.compile(rightLiTpl)({v: data}));
                    // 基本信息手机字段特殊处理
                    if (curPagePos >= 0 && curFieldPos >= 0) {
                        var firstUl = $('ul:eq(0)', '#selected-fields');
                        var firstUlLis = firstUl.find('li');
                        if (firstUlLis.length == 0) {
                            firstUl.append(rightLi);
                        } else {
                            firstUlLis.each(function (i) {
                                var me = $(this);
                                var liField = me.data('field');

                                var liPos = $.inArray(liField, specialFields);
                                if (curFieldPos < liPos) {
                                    rightLi.insertBefore(me);
                                    return false;
                                } else if (i == firstUlLis.length - 1) {
                                    firstUl.append(rightLi);
                                    return false;
                                }
                            });
                        }
                    } else {
                        $('#selected-fields ul:last-child').append(rightLi);
                        $('#selected-fields').scrollTop(100000000);
                        //rightLi.addClass('highlight');
                        rightLi.animate({
                            'border-color': '#ff7713'
                        }, 600, function () {
                            rightLi.animate({
                                'border-color': 'transparent',
                                'border-bottom-color': '#ddd'
                            }, 400);
                        });
                    }
                    ele.hide();
                }

                // 从已选字段移至备选字段
                function move2Left(ele) {
                    var data = ele.data();
                    var field = data.field;
                    var hideLi = $('#avai-fields li[data-field="' + field + '"]');
                    if (hideLi.length == 0) {
                        var leftLi = template.compile(leftLiTpl)({v: data});
                        $('#avai-fields').append(leftLi);
                    } else {
                        hideLi.show();
                    }
                    ele.remove();
                }

                $scope.$watch('curProjectId',function(newValue,oldValue){
                    if(newValue !== oldValue){
                        initSettingsData();
                    }
                });


            }
        ]);

    angular.bootstrap(document, ['app']);
});
